package com.lesshassles.model;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TaskBrokerImpl extends HibernateDaoSupport implements TaskBroker {
	public List<Task> getTasksAssignedToUser(int userId) {
		return (List<Task>) getSession()
				.createQuery("from Task where assignedTo = :userId")
				.setInteger("userId", userId)
				.list();
	}

	public List<Task> getTasksOpenedByUser(int userId) {
		return (List<Task>) getSession()
				.createQuery("from Task where createdBy = :userId and assignedTo <> :userId order by dueDate asc")
				.setInteger("userId", userId)
				.list();
	}

	public Task getById(int taskId) {
		return (Task) getSession()
				.createQuery("from Task where id = :taskId ")
				.setInteger("taskId", taskId)
				.uniqueResult();
	}
	
	public Task save(Task task) throws Exception {
		try {
			getSession().save(task);
			return task;
		} catch(Exception ex) {
			throw ex;
		}
	}

}
